package admin

import (
	"context"

	"server/internal/svc"
	"server/internal/types"
	"server/models"
	"server/utils"

	"github.com/zeromicro/go-zero/core/logx"
)

type AdminListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAdminListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminListLogic {
	return &AdminListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AdminListLogic) AdminList(req *types.AdminListReq) (resp *types.AdminListResp, err error) {

	tx := utils.Db.Model(&models.Admin{}).Where("id > ? ", 1)
	if req.Keyword != "" {
		tx.Where("username LIKE ?", "%"+req.Keyword+"%")
	}

	total := utils.TxCount(tx)
	if total == 0 {
		return &types.AdminListResp{
			Total: total,
			List:  []types.AdminListItem{},
		}, nil
	}
	tx = tx.Order("id desc")
	datas := make([]types.AdminListItem, 0)
	utils.TxPage(tx, req.Page, &datas)
	resp = &types.AdminListResp{
		Total: total,
		List:  datas,
	}
	return resp, nil
}
